package com.ledim.bean;

import android.text.TextUtils;
import com.ledim.service.coverter.LedimGsonConverterFactory;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.LogUtil;
import java.io.Serializable;
import org.json.JSONObject;

/**
 * Created by ccx on 16/7/26.
 * 聊天室消息实体
 */
public class LedimChatMsgBean implements Serializable {

    public String roomId;
    public String message;
    public String msgid;
    public LedimChatUserBean user;
    public long addtime;
    public long timestamp;

    public String color;
    // public String extend;
    // public String remark;

    public String uid;
    public int type;
    public int coin;

    public ChatExtend extend;

    public static class ChatExtend implements Serializable {

        public String remark;
    }

    public LedimChatMsgBean getRealChatMsgBean() {
        try {
            if (extend != null && !TextUtils.isEmpty(extend.remark)) {
                LogUtil.e("ccx msg extend===" + extend.remark);
                JSONObject remarkjson = new JSONObject(extend.remark);
                this.type = remarkjson.optInt("type");
                LogUtil.e("ccx msg type===" + type);
                if (type == ConstantUtil.ChatType.icon) {
                    LogUtil.e("ccx msg icon===" + remarkjson.optString("message"));
                    this.message = remarkjson.optString("message");

                }
                this.coin = remarkjson.optInt("coin");
                this.uid = remarkjson.optString("uid");
                this.timestamp = remarkjson.optLong("timestamp");
                String user_role = remarkjson.optString("user_role");
                if (!TextUtils.isEmpty(user_role) && user != null) {
                    LedimChatUserRole userRole =
                            LedimGsonConverterFactory.create().gson.fromJson(remarkjson.optString("user_role"), LedimChatUserRole.class);
                    if (userRole != null) {
                        if (!TextUtils.isEmpty(userRole.nickname)) {
                            this.user.nickname = userRole.nickname;
                        }
                        if (!TextUtils.isEmpty(userRole.avatar)) {
                            this.user.icon = userRole.avatar;
                        }
                    }
                }
            }
            if (user != null) {
                return this;
            }
        } catch (Exception e) {
            LogUtil.e("ccx mgs exception==" + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

}
